package com.bawei.http

import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.cache.CacheManager
import com.bawei.cache.CacheStrategy
import okhttp3.Interceptor
import okhttp3.Response


/**
 * 作者：Jason
 * 日期：2023/11/13
 * 描述：Token拦截器
 */
class TokenInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        var token =  CacheManager.getCache(CacheStrategy.MMKV).getString("token")
        if(!token.isNullOrEmpty()){
            var newRequest = chain.request().newBuilder().header("token", token).build()
            var response = chain.proceed(newRequest)
            //登录失效，需要跳转到登录页面
            if (response.code ==  403) {
                ARouter.getInstance().build("/activity/login").navigation()
            }
            return response
        }

        return chain.proceed(chain.request())

    }

}